<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Configuration
 *
 * @author MinhNguyen
 */

class Like {
    //put your code here
    public $id;
    public $statusid;
    public $userid;
    public $datecreated;
    
    public static $instance;
    
    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }


    public function __get($name) {
        return $this->$name;
    }
    
    public function __set($name, $value) {
        $this->$name = $value;
    }   

    /**
     * It's constuctor of User with email
     */
    
    public  function __construct() {
        //allocate your stuff
    }

    
    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $query = "SELECT * FROM likes WHERE id = '$id'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->id = $rows["id"];
                $instance->statusid = $rows["statusid"];
                $instance->userid = $rows["userid"];
                $instance->datecreated = $rows["datecreated"];
                return $instance;
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView(), 
                    "404", "Not found any user with provided email");
                $controller->returnError();
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }  
    
    public static function withData($statusid, $userid, $datecreated) {
        $instance = new self();
        $instance->statusid = $statusid;
        $instance->userid = $userid;
        $instance->datecreated = $datecreated;
        return $instance;
    }
    
    public static function addLike($statusId, $userId, $dateCreated){
        $db = Database::getInstance();
        $id = $db->getID("likes", "id");
        $statusid = $statusId;
        $userid = $userId;
        $datecreated = $dateCreated;
        if(!Like::checkLike($statusid, $userid)){
            $query = "INSERT INTO likes (id, statusid, userid, datecreated) VALUES 
                    ('$id', '$statusid', '$userid', '$datecreated')";
            $result = $db->query($query);
            if ($result->returnCode == 200) {
                Like::increaseLike($statusid);
                return $id;
            }else
                return 0;
        }else{
            return 0;
        }
    }
    
    public static function getAmountLike($statusId){
        $db = Database::getInstance();
        $query = "SELECT COUNT(id) FROM likes WHERE statusid = '$statusId'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            $row = $result->dataTable->rows[0];
            return $row[0];
        }else
            return -1;
    }
    
    public static function increaseLike($statusId){
        $status = Status::withId($statusId);
        $value = $status->likes + 1;
        Status::updateColumn("likes", $value, $status->id);
    }
    
     public static function checkLike($statusid, $userid){
        $db = Database::getInstance();
        $query = "SELECT id FROM likes WHERE statusid = '$statusid' AND userid='$userid'";
        $result = $db->query($query);
        if ($result->returnCode == 200)
            if ($result->dataTable->numOfRow == 1) 
                return true;
            else
                return false;
        else
            return false;
    }
    
    public static function deleteLike($statusid, $userid){
        $db = Database::getInstance();
        $query = "DELETE FROM likes WHERE statusid = '$statusid' AND userid='$userid'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            return "true";
        }
        else
            return "false";
    }
}
?>
